package com.huiminpay.transaction.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@Controller
public class PayTestController {

    //应用id
    String APP_ID = "2021000116660509";
    //应用私钥
    String APP_PRIVATE_KEY = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQClPulKKFAEWw0P2MWhqzEipTulYXzqvtMKv5tS7V+uNijfxayG5BnAK36mMgCjAWYi81Ikt8MiS+QdH4l24BU0s1M4nXMHSqfg0PHnpaoLtHTpv5LiWW4uY928IPV60hIk2Cy7m2J58rsJveJ0SftulLiOeMyBMvkRZNYtuuto9ABUXYcPltxMQSKQBeoHewnceA8LKs7Fj3Zk7mXikpmFCsYuqI5zmUjDPFcda+A7599Cwb3pTCNfHtGJpMGw3FGvIyuN7HSLKzktrWCBDj1bv7mSWLZRegm4HlcNXY86kGYLG2tS7QiTWXKT3Foxbrg+wDsCbjDU0XGX2UF9ntFzAgMBAAECggEAai6CuaQtxp67RfOFCt12NUz3hs2d9zmnYsC8jgz57nfey+u5cPQhl9pbB3AZ+KRtfQfdQLrUKLWbXxtkuX5EIhkY4MhLoePW/B9HUhKhsePDPHBdchUFN8n0MW78Tm+uOP6vv92Y6cOPbDMX0KG5btUhwmrc/ihC42poNfpmSol1WJC80SYlqeJ3o6z/At7rBoE9JmuzxAmt51Pfbl+xVpbIfMOzVEXX60IdJXgUF8BNssGNqd01HQucRaQQVgLQEIggofCcVtWrpeaTFtGO/WolCflBWEvcVEmwOA7m7GU5IhhcrHvZU+FNbeHoW7G9sErxqJusHOt/2VyTRun8AQKBgQDomPJQ8s9qKKy9kTPi42YSGzpOxWxuGL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VF2HD5bcTEEikAXqB3sJ3HgPCyrOxY92ZO5l4pKZhQrGLqiOc5lIwzxXHWvgO+ffQsG96UwjXx7RiaTBsNxRryMrjex0iys5La1ggQ49W7+5kli2UXoJuB5XDV2POpBmCxtrUu0Ik1lyk9xaMW64PsA7Am4w1NFxl9lBfZ7RcwIDAQAB";
    String CHARSET = "utf-8";
    //支付宝接口的网关地址，正式"https://openapi.alipay.com/gateway.do"
    String serverUrl = "https://openapi.alipaydev.com/gateway.do";
    //签名算法类型
    String sign_type = "RSA2";

    @GetMapping("/alipaytest")
    public void alipaytest(HttpServletRequest httpRequest,
                           HttpServletResponse httpResponse) throws ServletException, IOException {
        //构造sdk的客户端对象
        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, APP_ID, APP_PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, sign_type); //获得初始化的AlipayClient
        AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();//创建API对应的request
//        alipayRequest.setReturnUrl("http://domain.com/CallBack/return_url.jsp");
//        alipayRequest.setNotifyUrl("http://domain.com/CallBack/notify_url.jsp");//在公共参数中设置回跳和通知地址
        alipayRequest.setBizContent("{" +
                //订单号需修改
                " \"out_trade_no\":\"20150420010101020\"," +
                " \"total_amount\":\"450000\"," +
                " \"subject\":\"凯迪拉克XT5\"," +
                " \"product_code\":\"QUICK_WAP_PAY\"" +
                " }");//填充业务参数
        String form = "";
        try {
            //请求支付宝下单接口,发起http请求
            form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        httpResponse.setContentType("text/html;charset=" + CHARSET);
        httpResponse.getWriter().write(form);//直接将完整的表单html输出到页面
        httpResponse.getWriter().flush();
        httpResponse.getWriter().close();
    }
}
